//
//  LLPImageFileCache.m
//  LLP
//
//  Created by pan Shiyu on 12-10-14.
//  Copyright (c) 2012年 Porcellio. All rights reserved.
//

#import "LLPImageFileCache.h"
#import "CMFileCache.h"

static LLPImageFileCache* _myLLPImageFileCache = nil;
@implementation LLPImageFileCache

+ (LLPImageFileCache*)sharedImageFileCache{
    if (_myLLPImageFileCache == nil) {
        _myLLPImageFileCache = [[LLPImageFileCache alloc]init];
    }
    return _myLLPImageFileCache;
}

- (void)dealloc{
    [_cacheName release];
    [_cachePath release];
    [super dealloc];
}
- (id)init{
    self = [super init];
    if (self) {
       _cacheName = [@"images/" retain];
        _cachePath = [[[CMFileCache shareFileCache]filePathWithName:_cacheName] retain];
    }
    return self;
}

#pragma mark - 
- (BOOL)hasImgFile:(NSString*)fName{
    NSString* fpath = [self imgFilePathByName:fName];
    return [[CMFileCache shareFileCache]hasData:fpath];
}
- (NSString*)imgFilePathByName:(NSString*)fName{
    return  [_cachePath stringByAppendingPathComponent:fName];
}

#pragma mark -

- (void)saveImgFile:(NSData*)fData withName:(NSString*)fName{
    if (fData == nil || fData.length == 0) {
        return;
    }
    NSString* fpath = [self imgFilePathByName:fName];
    [[CMFileCache shareFileCache]saveFile:fData withPath:fpath];
}
- (UIImage*)imgFileDataByName:(NSString*)fName{
    NSString* fpath = [self imgFilePathByName:fName];
    NSData* imgData = [[CMFileCache shareFileCache]fileDataByPath:fpath];
    if (imgData) {
        return [UIImage imageWithData:imgData];
    }
    return nil;
}
- (void)removeFileByName:(NSString*)fName{
    NSString* fpath = [self imgFilePathByName:fName];
    [[CMFileCache shareFileCache]removeFileByPath:fpath];
}

@end
